package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.DemandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/demand")
public class DemandController {
    @Autowired
    DemandService demandService;

    /**
     * 客户需求分页查询
     * @param search
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/demand-list")
    @ResponseBody
    public Map<String,Object> demandList(String search,String type, Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        demandService.selectAllDemand(search,type,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 新增客户需求
     * @param demand
     */
    @RequestMapping("/addDemand")
    @ResponseBody
    public void addDemand(@RequestBody Demand demand){
        demandService.addDemand(demand);
    }

    /**
     * 查询需要修改的客户需求
     * @param demandId
     * @return
     */
    @RequestMapping("/upDemand")
    @ResponseBody
    public List<Demand> upDemand(Integer demandId){
        return demandService.upDemand(demandId);
    }

    /**
     * 删除客户需求
     * @param demandId
     */
    @RequestMapping("/delDemand")
    @ResponseBody
    public void delDemand(Integer demandId){
        demandService.delDemand(demandId);
    }

    /**
     * 客户需求客户列表
     * @param khsearch
     * @return
     */
    @RequestMapping("/kh-list")
    @ResponseBody
    public Map<String,Object> selectKh(String khsearch,Integer pageNum, Integer pageSize,String empName){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        demandService.selectKh(khsearch,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 对应机会列表
     * @param khName
     * @return
     */
    @RequestMapping("/sales-list")
    @ResponseBody
    public List<Sales> selectSales(String khName){
        return demandService.selectSales(khName);
    }


    /**
     * 重要程度列表
     * @return
     */
    @RequestMapping("/degree-list")
    @ResponseBody
    public List<Degree> selectdegree(){
        return demandService.selectdegree();
    }

    /**
     * 销售机会页面已生成客户需求不可删除
     * @param salesId
     * @return
     */
    @RequestMapping("/selectSalesDemand")
    @ResponseBody
    public List<Demand> selectSalesDemand(Integer salesId){
        return demandService.selectSalesDemand(salesId);
    }
}
